<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消防员id" prop="firefighterId">
        <el-input
          v-model="queryParams.firefighterId"
          placeholder="请输入消防员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消防员证号" prop="firefighterIdCard">
        <el-input
          v-model="queryParams.firefighterIdCard"
          placeholder="请输入消防员证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="毕业院校" prop="graduationSchoolName">
        <el-input
          v-model="queryParams.graduationSchoolName"
          placeholder="请输入毕业院校"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所学专业" prop="major">
        <el-input
          v-model="queryParams.major"
          placeholder="请输入所学专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入学日期" prop="enrollmentDate">
        <el-date-picker clearable
                        v-model="queryParams.enrollmentDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入学日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="毕业日期" prop="graduationDate">
        <el-date-picker clearable
                        v-model="queryParams.graduationDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择毕业日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="消防员姓名" prop="firefighterName">
        <el-input
          v-model="queryParams.firefighterName"
          placeholder="请输入消防员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:school:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:school:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:school:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:school:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="消防员id" align="center" prop="firefighterId" />
      <el-table-column label="消防员证号" align="center" prop="firefighterIdCard" />
      <el-table-column label="毕业院校" align="center" prop="graduationSchoolName" />
      <el-table-column label="所学专业" align="center" prop="major" />
      <el-table-column label="入学日期" align="center" prop="enrollmentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="毕业日期" align="center" prop="graduationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.graduationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消防员姓名" align="center" prop="firefighterName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:school:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:school:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改毕业院校信息对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消防员证号" prop="firefighterIdCard">
          <el-input v-model="form.firefighterIdCard" placeholder="请输入消防员证号" />
        </el-form-item>
        <el-form-item label="毕业院校" prop="graduationSchoolName">
          <el-input v-model="form.graduationSchoolName" placeholder="请输入毕业院校" />
        </el-form-item>
        <el-form-item label="所学专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入所学专业" />
        </el-form-item>
        <el-form-item label="入学日期" prop="enrollmentDate">
          <el-date-picker clearable
                          v-model="form.enrollmentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择入学日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="毕业日期" prop="graduationDate">
          <el-date-picker clearable
                          v-model="form.graduationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择毕业日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消防员姓名" prop="firefighterName">
          <el-input v-model="form.firefighterName" placeholder="请输入消防员姓名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
			<el-form ref="form" :model="form" :rules="rules"   @submit.native.prevent>

				<div style="width: 100%; display: flex;justify-content: flex-end;padding-bottom: 20px;" v-if="form.id == null">
					<el-button @click="addInput">添加毕业院校</el-button>
				</div>

        <div v-if="form.id == null">
          <div v-for="(item, i) in form.firefighterAwards" :key="i" >
            <div style="padding-bottom: 20px;">
              <div style="display: flex;justify-content: space-between;">
                <div>
                  <span style="margin-right: 20px;">开始日期</span>
                  <el-date-picker @change="addsStartDate(i, $event)"
                    v-model="item.enrollmentDate"
                    type="date"
                    placeholder="选择日期">
                  </el-date-picker>
                </div>

                <div>
                  <span style="margin-right: 20px;">结束日期</span>
                  <el-date-picker @change="addsEndDate(i, $event)"
                    v-model="item.graduationDate"
                    type="date"
                    placeholder="选择日期">
                  </el-date-picker>
                </div>
              </div>

              <el-form-item  prop="position" >
                <span class="biaoti">专业</span>
                <el-input :value="item.major" placeholder="请输入" @input="addsUnit(i, $event)" />
              </el-form-item>


              <el-form-item  prop="position" >
                <span class="biaoti">学校名称</span>
                <el-input  :value="item.graduationSchoolName" placeholder="请输入" @input="addsPosition(i, $event)" />
              </el-form-item>
              

              <!-- <el-select v-model="item." placeholder="请选择" @change="addsLevel(i,$event)">
                <el-option v-for="item in unit" :key="item.rewardId" :label="item.rewardName"
                  :value="item.rewardId">
                </el-option>
              </el-select> -->
          
              <!-- <el-date-picker @change="addsMonth(i, $event)" v-model="item.time" type="month"
                placeholder="选择月">
              </el-date-picker> -->

              
            </div>
          </div>
        </div>


        
        <el-form-item label="" prop="firefighterName"  v-if="form.id != null">
					<span class="biaoti">学校名称</span>
					<el-input v-model="form.firefighterAwards[0].graduationSchoolName" placeholder="请输入" />
				</el-form-item>

        <el-form-item label="" prop="firefighterName" v-if="form.id != null">
					<span class="biaoti">专业</span>
					<el-input v-model="form.firefighterAwards[0].major" placeholder="请输入" />
				</el-form-item>

        <el-form-item label="" prop="firefighterName" v-if="form.id != null">
					<span class="biaoti">开始时间</span>
					<el-input v-model="form.firefighterAwards[0].enrollmentDate" placeholder="请输入" />
				</el-form-item>

        <el-form-item label="" prop="firefighterName" v-if="form.id != null">
					<span class="biaoti">结束时间</span>
					<el-input v-model="form.firefighterAwards[0].graduationDate" placeholder="请输入" />
				</el-form-item>



				<el-form-item label="" prop="firefighterName">
					<span class="biaoti">消防员姓名</span>
					<el-input v-model="form.firefighterName" placeholder="请输入" />
				</el-form-item>
				
				<el-form-item label="" prop="firefighterIdCard">
					<span class="biaoti">消防员证件号</span>
					<el-input v-model="form.firefighterIdCard" placeholder="请输入"
						@change="getFirefighterIdCard" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
import { listSchool, getSchool, delSchool, addSchool, updateSchool,getFirefighterIdCard,addList } from "@/api/graduation/graduation";

export default {
  name: "School",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 毕业院校信息表格数据
      schoolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        firefighterId: null,
        firefighterIdCard: null,
        graduationSchoolName: null,
        major: null,
        enrollmentDate: null,
        graduationDate: null,
        firefighterName: null
      },
      // 表单参数
      form: {
        pageNum: 1,
        pageSize: 10,
        firefighterName: null,
        firefighterIdCard: null,
        firefighterAwards: [
          {
            graduationSchoolName:null,
            major:null,
            enrollmentDate:null,
            graduationDate:null,
          }
        ]
      },
      // 表单校验
      rules: {
        // firefighterId: [
        //   { required: true, message: "消防员id不能为空", trigger: "blur" }
        // ],
        // firefighterIdCard: [
        //   { required: true, message: "消防员证号不能为空", trigger: "blur" }
        // ],
        // graduationSchoolName: [
        //   { required: true, message: "毕业院校不能为空", trigger: "blur" }
        // ],
        // major: [
        //   { required: true, message: "所学专业不能为空", trigger: "blur" }
        // ],
        // enrollmentDate: [
        //   { required: true, message: "入学日期不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getFirefighterIdCard(nameId) {
				console.log("获取证件号：", nameId)
				getFirefighterIdCard(nameId).then(res => {
					console.log("证件号：", res)
					this.form.firefighterName = res.data.name
				})
			},
			// 批量添加
			addsUpdate() {
				console.log("表单数据：", this.form)
				addAwardList(this.form).then(res => {
					console.log("返回结果：", res)
				})
				this.getList();
			},
			addInput() {
				// 向inputs数组中添加一个新的输入框对象
				this.form.firefighterAwards.push({
					graduationSchoolName: "",
						major: "",
						enrollmentDate: "",
            graduationDate:"",
				});
			},
			// 更新特定输入框的值
			addsUnit(i, value) {
				console.log(value)
				this.form.firefighterAwards[i].major = value;
			},
      addsPosition(i, value) {
				console.log(value)
				this.form.firefighterAwards[i].graduationSchoolName = value;
			},
			addsStartDate(i, e) {
        console.log("时间：",e)
				this.form.firefighterAwards[i].enrollmentDate = this.dateUpdata(e)
			},

			addsEndDate(i, e) {
        console.log("时间：",e)
				this.form.firefighterAwards[i].graduationDate = this.dateUpdata(e)
			},
			MonthUpdate(e) {
				var formattedDate = e;
				// 解析日期时间字符串为Date对象
				const date = new Date(formattedDate);
				// 获取年份
				const year = date.getFullYear();
				// 获取月份（0-11，所以要加1）
				const month = date.getMonth() + 1;
				// 格式化月份为两位数字
				const formattedMonth = month < 10 ? '0' + month : month;
				// 构建最终的日期字符串
				formattedDate = `${year}-${formattedMonth}`;
				console.log(formattedDate); // 输出: 2024-10
				return formattedDate
			},
      dateUpdata(e){
        const dateString = e;
        // 解析日期时间字符串为 Date 对象
        const date = new Date(dateString);
        // 获取年、月、日
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以加1
        const day = date.getDate().toString().padStart(2, '0');
        // 格式化为 yyyy-mm-dd 格式
        const formattedDate = `${year}-${month}-${day}`;
        console.log(formattedDate); // 输出: 2024-11-03
        return formattedDate;
      },
    /** 查询毕业院校信息列表 */
    getList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
          firefighterName: "",
					firefighterIdCard: "",
					firefighterAwards: [{
						graduationSchoolName: "",
						major: "",
						enrollmentDate: "",
            graduationDate:"",
					}, ],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加毕业院校信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchool(id).then(response => {
        // this.form = response.data;
        this.form.id = response.data.id;
        this.form.firefighterName = response.data.firefighterName;
          this.form.firefighterIdCard = response.data.firefighterIdCard;
          this.form.firefighterAwards[0].graduationSchoolName = response.data.graduationSchoolName;
          this.form.firefighterAwards[0].major = response.data.major;
          this.form.firefighterAwards[0].enrollmentDate = response.data.enrollmentDate;
          this.form.firefighterAwards[0].graduationDate = response.data.graduationDate;
        this.open = true;
        this.title = "修改毕业院校信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除毕业院校信息编号为"' + ids + '"的数据项？').then(function() {
        return delSchool(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/school/export', {
        ...this.queryParams
      }, `school_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
